<template>
  <div class="details">
    <el-card>
      <div class="print"><el-button v-if="active!='first'" size="mini" @click="toPrint">打印</el-button></div>
      <el-tabs v-model="active">
        <el-tab-pane label="登录账号设置" name="first">
          <el-form :model="formData" class="myform" label-width="100px">
            <el-form-item label="姓名">
              <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input type="password" v-model="formData.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="submit" type="primary">更新</el-button>
              <el-button type="text">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userInfo :userInfo="userInfo"></userInfo>
        </el-tab-pane>
        <el-tab-pane label="岗位详情" name="third">
          <jobInfo></jobInfo>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUserInfoApi, updateUserInfoApi } from "@/api/employees";
export default {
  components: {
    userInfo: () => import("../components/user-info.vue"),
    jobInfo: () => import("../components/job-info.vue")
  },
  data() {
    return {
      active: "first",
      // 数据源
      formData: {
        username: "",
        password: ""
      },
      //   当前查看用户的id
      id: this.$route.query.id,
      //  保存当前用户的信息
      userInfo: {}
    };
  },
  methods: {
    // 得到用户 的基本信息
    async getUserInfo() {
      console.log(this.id);
      const res = await getUserInfoApi(this.id);
      // 保存用户信息
      this.userInfo = res;
      // 赋值给input框
      this.formData.username = res.username;
      this.formData.password = res.password;
    },

    // 修改用户信息
    async submit() {
      try {
        await updateUserInfoApi({
          ...this.userInfo,
          ...this.formData
        });
        // 提示修改成功
        this.$message.success("更新用户信息成功");
      } catch {
        console.log("更新用户信息失败");
      }
    },

    // 跳转到打印页面
    toPrint() {
      this.$router.push(`/employees/print/${this.id}?type=${this.active}`);
    }
  },

  created() {
    this.getUserInfo(); // 拿用户信息
  }
};
</script>

<style lang="scss" scoped>
.details {
  padding: 20px;
  position: relative;
  .myform {
    width: 500px;
    margin: 50px 0px 50px 200px;
  }
  .print {
    position: absolute;
    top: 40px;
    right: 50px;
    z-index: 9999;
  }
}
</style>
